package com.uenpay.im.ui.old.activity.area;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;


/**
 * @author SL
 * @created 2016-1-28 下午3:01:39
 * @copyRight http://www.qiyunxin.com
 * @function 地区列表
 */
public class AreaActivity extends BaseActivity {

    private AreaHandle areaHandle = new AreaHandle();
    private ListView listview;
    private AreaAdapter mAreaAdapter = null;
    private String nowLevel = "p";
    private String nowPeare = "";
    private View loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_list_layout;
    }

    @Override
    public void initView(View view) {
        nowLevel = getIntent().getStringExtra("level");
        nowPeare = getIntent().getStringExtra("peare");
        if (TextUtils.isEmpty(nowLevel)) {
            nowLevel = "p";
        }
        initView();
        initListener();
        loading.setVisibility(View.VISIBLE);
        getData();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview)).setText(R.string.area);
        loading = findViewById(R.id.loading);
        listview = (ListView) findViewById(R.id.listview);
    }

    protected void initListener() {
        findViewById(R.id.back_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        finish();
                    }
                });

        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                AreaEntity mAreaEntity = (AreaEntity) arg0.getAdapter()
                        .getItem(arg2);
                if (mAreaEntity != null) {
                    String level = "";
                    if (nowLevel.equals("p")) {
                        level = "c";
                    } else if (nowLevel.equals("c")) {
                        if (mAreaEntity.isleaf == 1) {
                            Intent resultIntent = new Intent();
                            resultIntent.putExtra("result", nowPeare + "-"
                                    + mAreaEntity.area);
                            setResult(RESULT_OK, resultIntent);
                            finish();
                            return;
                        }
                        level = "a";
                    } else if (nowLevel.equals("a")) {
                        Intent resultIntent = new Intent();
                        resultIntent.putExtra("result", nowPeare + "-"
                                + mAreaEntity.area);
                        setResult(RESULT_OK, resultIntent);
                        finish();
                        return;
                    }
                    Bundle bundle = new Bundle();
                    bundle.putString("level", level);
                    String peare = mAreaEntity.area;
                    if (!TextUtils.isEmpty(nowPeare)) {
                        peare = nowPeare + "-" + mAreaEntity.area;
                    }
                    bundle.putString("peare", peare);
                    ActivityHandle.startActivity(AreaActivity.this,
                            AreaActivity.class, bundle, 100);
                }
            }
        });
    }

    private void getData() {
        areaHandle.getArea(nowLevel, nowPeare, new AreaHandle.IGetAreaResultListener() {

            @Override
            public void onGetAreaResult(int status, String msg,
                                        ArrayList<AreaEntity> arrayList) {
                loading.setVisibility(View.GONE);
                mAreaAdapter = new AreaAdapter(AreaActivity.this, arrayList);
                listview.setAdapter(mAreaAdapter);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100 && resultCode == -1 && data != null) {
            String result = data.getStringExtra("result");
            Intent resultIntent = new Intent();
            resultIntent.putExtra("result", result);
            setResult(RESULT_OK, resultIntent);
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }

}
